

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

#define DEVICE_TREE_PATH "/proc/device-tree"


int main(int argc, char **argv)
{
    printf("Reading device tree from: %s\n", DEVICE_TREE_PATH);
    read_device_tree(DEVICE_TREE_PATH, 0);

	return 0;
}
void print_indentation(int depth) {
    for (int i = 0; i < depth; ++i) {
        printf("--"); // 表示一个层级
    }
}
void read_device_tree(const char *path, int depth) {
    DIR *dir;
    struct dirent *entry;
    struct stat statbuf;

    if ((dir = opendir(path)) == NULL) {
        perror("opendir");
        return;
    }

    chdir(path);
    while ((entry = readdir(dir)) != NULL) {
        lstat(entry->d_name, &statbuf);
        if (S_ISDIR(statbuf.st_mode)) {
            // 忽略 . 和 ..
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            // 打印目录名并递归遍历子目录
            print_indentation(depth);
            printf("Directory: %s/\n", entry->d_name);
            read_device_tree(entry->d_name, depth + 1);
        } else {
            // 打印文件路径和内容
            print_indentation(depth);
            printf("File: %s \t: ", entry->d_name);
            FILE *file = fopen(entry->d_name, "r");
            if (file) {
                char buffer[256];
                size_t bytesRead = fread(buffer, 1, sizeof(buffer) - 1, file);
                buffer[bytesRead] = '\0';
                printf("\"%s\"\n", buffer);
                fclose(file);
            } else {
                perror("fopen");
            }
        }
    }
    chdir("..");
    closedir(dir);
}

